package com.quktzhuanka.app.my;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.JdException;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.quktzhuanka.app.R;
import com.quktzhuanka.app.activity.JdDetailsActivity;
import com.quktzhuanka.app.activity.PddDetailsActivity;
import com.quktzhuanka.app.activity.PromotionDetailsActivity;
import com.quktzhuanka.app.adapter.JDAdapterList;
import com.quktzhuanka.app.adapter.PddRecyclerAdapter;
import com.quktzhuanka.app.adapter.ShopRecyclerAdapter;
import com.quktzhuanka.app.base.BaseActivity;
import com.quktzhuanka.app.bean.JdGoodsResp;
import com.quktzhuanka.app.bean.PDDBean;
import com.quktzhuanka.app.bean.PddClient;
import com.quktzhuanka.app.bean.Response;
import com.quktzhuanka.app.bean.TaobaoGuestBean;
import com.quktzhuanka.app.common.SPUtils;
import com.quktzhuanka.app.common.T;
import com.quktzhuanka.app.config.Constants;
import com.quktzhuanka.app.https.HttpUtils;
import com.quktzhuanka.app.https.onOKJsonHttpResponseHandler;
import com.quktzhuanka.app.login.WelActivity;
import com.quktzhuanka.app.utils.SlideRecyclerView;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

/**
 * @属性:我的收藏
 * @开发者:wmm
 * @时间:2018/12/11 11:10
 */
public class CollectionActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    SlideRecyclerView recyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.rg_type)
    RadioGroup rgType;
    @BindView(R.id.recyclerView_jd)
    SlideRecyclerView recyclerViewJd;
    @BindView(R.id.recyclerView_pdd)
    SlideRecyclerView recyclerViewPdd;
    private View view;
    public static CollectionActivity fragment;
    private ShopRecyclerAdapter shopRecyclerAdapter;
    List<TaobaoGuestBean.TaobaoGuesChildtBean> taobaoGuesChildtBeans = new ArrayList<>();

    private JDAdapterList jdAdapterList;
    List<JdGoodsResp> jdList = new ArrayList<>();
    private GridLayoutManager gridrLayoutManager;


    private PddRecyclerAdapter pddAdapter;
    List<PDDBean> pddList = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;
    DecimalFormat df=new DecimalFormat("0.00");
    Gson gson=new Gson();
    private int page1=1,page2=1,page3=1;
    @Override
    protected void initUI() {
        setContentView(R.layout.fragment_consultation);
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        //设置标题
        tvTitle.setText("我的收藏");
        tvLeft.setVisibility(View.VISIBLE);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //适配器
        shopRecyclerAdapter = new ShopRecyclerAdapter(this, R.layout.today_highlights_child_item, taobaoGuesChildtBeans);
        //管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        //添加分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(shopRecyclerAdapter);
        //取消加载
        //开始刷新
        refreshLayout.autoRefresh();


        jdAdapterList = new JDAdapterList(this, R.layout.item_jd, jdList);
        gridrLayoutManager = new GridLayoutManager(this, 1);
        gridrLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewJd.setLayoutManager(gridrLayoutManager);
        recyclerViewJd.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerViewJd.setAdapter(jdAdapterList);

        pddAdapter = new PddRecyclerAdapter(this, R.layout.pdd_item, pddList);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewPdd.setLayoutManager(linearLayoutManager);
        recyclerViewPdd.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerViewPdd.setAdapter(pddAdapter);
    }

    @Override
    protected void initListener() {
        shopRecyclerAdapter.setOnDeleteClickListener(new ShopRecyclerAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                cancelCollectRequest(1,position);
            }
        });
        jdAdapterList.setOnDeleteClickListener(new ShopRecyclerAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                cancelCollectRequest(3,position);
            }
        });
        pddAdapter.setOnDeleteClickListener(new ShopRecyclerAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                cancelCollectRequest(2,position);
            }
        });
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (TextUtils.isEmpty(SPUtils.getStringData(CollectionActivity.this, Constants.TOKEN, ""))) {
                    openActivity(WelActivity.class);
                    refreshLayout.finishRefresh();
                    return;
                }
                if(recyclerView.getVisibility()==View.VISIBLE){
                    page1++;
                    getTbkListRequst();
                }else if(recyclerViewJd.getVisibility()==View.VISIBLE){
                    page2++;
                    getJdCollect();
                }else if(recyclerViewPdd.getVisibility()==View.VISIBLE){
                    page3++;
                    getPddCollect();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (TextUtils.isEmpty(SPUtils.getStringData(CollectionActivity.this, Constants.TOKEN, ""))) {
                    openActivity(WelActivity.class);
                    refreshLayout.finishRefresh();
                    return;
                }
                if(recyclerView.getVisibility()==View.VISIBLE){
                    page1=1;
                    getTbkListRequst();
                }else if(recyclerViewJd.getVisibility()==View.VISIBLE){
                    page2=1;
                    getJdCollect();
                }else if(recyclerViewPdd.getVisibility()==View.VISIBLE){
                    page3=1;
                    getPddCollect();
                }
            }
        });
        //点击进入详情
        shopRecyclerAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                TaobaoGuestBean.TaobaoGuesChildtBean taobaoGuesChildtBean = taobaoGuesChildtBeans.get(position);
                if (taobaoGuesChildtBean != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("num_iid", taobaoGuesChildtBean.getNum_iid());
                    openActivity(PromotionDetailsActivity.class, bundle);
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        //点击进入详情
        jdAdapterList.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                JdGoodsResp taobaoGuesChildtBean = jdList.get(position);
                if (taobaoGuesChildtBean != null) {
                    Intent intent=new Intent(CollectionActivity.this,JdDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("goods", taobaoGuesChildtBean);
                    intent.putExtra("goods",bundle);
                    startActivity(intent);
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        //点击进入详情
        pddAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                PDDBean taobaoGuesChildtBean = pddList.get(position);
                if (taobaoGuesChildtBean != null) {
                    Intent intent=new Intent(CollectionActivity.this,PddDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("goods", taobaoGuesChildtBean);
                    intent.putExtra("goods",bundle);
                    startActivity(intent);
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        rgType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_my:
                        recyclerView.setVisibility(View.VISIBLE);
                        recyclerViewJd.setVisibility(View.GONE);
                        recyclerViewPdd.setVisibility(View.GONE);
                        if(taobaoGuesChildtBeans.size()<=0){
                            refreshLayout.autoRefresh();
                        }
                        break;
                    case R.id.rb_direct:
                        recyclerView.setVisibility(View.GONE);
                        recyclerViewJd.setVisibility(View.VISIBLE);
                        recyclerViewPdd.setVisibility(View.GONE);
                        if(jdList.size()<=0){
                            refreshLayout.autoRefresh();
                        }
                        break;
                    case R.id.rb_next:
                        recyclerView.setVisibility(View.GONE);
                        recyclerViewJd.setVisibility(View.GONE);
                        recyclerViewPdd.setVisibility(View.VISIBLE);
                        if(pddList.size()<=0){
                            refreshLayout.autoRefresh();
                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void ReceiverIsLoginMessage() {
        super.ReceiverIsLoginMessage();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (refreshLayout != null) {
                    refreshLayout.autoRefresh();
                }
            }
        }, 1000);
    }

    @Override
    protected void ReceiverBroadCastMessage(String status, String result, Serializable serializable, Intent intent) {
        super.ReceiverBroadCastMessage(status, result, serializable, intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 获取京东收藏ids
     */
    private void getJdCollect(){
        RequestParams requestParams = new RequestParams();
        requestParams.put("p",page2);
        requestParams.put("per",6);
        HttpUtils.post(Constants.GETJD_COLLECT_LIST, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject object=new JSONObject(responseString);
                    if(object.getInt("code")==0){
                        if("".equals(object.getJSONObject("data").getString("goods_allid"))){
                            T.showShort(CollectionActivity.this,"暂无更多收藏");
                            if(page2==1)
                                jdList.clear();
                            jdAdapterList.notifyDataSetChanged();
                            refreshLayout.finishRefresh();
                            refreshLayout.finishLoadMore();
                            return;
                        }
//                        String[] strs=object.getJSONObject("data").getString("goods_allid").split(",");
//                        Long[] ids=new Long[strs.length];
//                        for(int i=0;i<strs.length;i++){
//                            ids[i]=Long.valueOf(strs[i]);
//                        }
                        getJdGoodsMsgRequest(object.getJSONObject("data").getString("goods_allid"));
                    }else{

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private void getPddCollect(){
        RequestParams requestParams = new RequestParams();
        requestParams.put("p",page3);
        requestParams.put("per",6);
        HttpUtils.post(Constants.GETPDD_COLLECT_LIST, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject object=new JSONObject(responseString);
                    if(object.getInt("code")==0){
                        if("".equals(object.getJSONObject("data").getString("goods_allid"))){
                            refreshLayout.finishRefresh();
                            refreshLayout.finishLoadMore();
                            T.showShort(CollectionActivity.this,"暂无更多收藏");
                            if(page3==1)
                            pddList.clear();
                            pddAdapter.notifyDataSetChanged();
                            return;
                        }
                        getPddList(object.getJSONObject("data").getString("goods_allid"));
                    }else{

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * @属性:获取Pdd商品列表
     * @开发者:wmm
     * @时间:2018/12/11 15:30
     */
    private void getPddList(String ids) {
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        RequestParams requestParams = new RequestParams();
        requestParams.put("data_type", PddClient.data_type);
        requestParams.put("client_id", PddClient.client_id);
        requestParams.put("timestamp", time);
        requestParams.put("with_coupon", "true");
        requestParams.put("goods_id_list", "["+ids+"]");
        requestParams.put("type", "pdd.ddk.goods.search");
        Map<String, Object> temp = new HashMap<>();
        temp.put("data_type", PddClient.data_type);
        temp.put("type", "pdd.ddk.goods.search");
        temp.put("client_id", PddClient.client_id);
        temp.put("timestamp", time);
        temp.put("goods_id_list",  "["+ids+"]");
        temp.put("with_coupon", "true");
        String sign = PddClient.getSign(temp);
        requestParams.put("sign", sign);
        HttpUtils.post1(PddClient.serverUrl, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (refreshLayout != null) {
                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if (responseString.contains("error_response")) {
                    return;
                }
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    JSONArray array = jsonObject.getJSONObject("goods_search_response").getJSONArray("goods_list");
                    JSONObject tempObj;
                    if(page3==1)
                    pddList.clear();
                    for (int i = 0; i < array.length(); i++) {
                        tempObj = array.getJSONObject(i);
                        double tem = (Double.valueOf(tempObj.getString("min_group_price")) - Double.valueOf(tempObj.getString("coupon_discount"))) * Double.valueOf(df.format(Double.valueOf(tempObj.getString("promotion_rate")) / 1000));
                        tempObj.put("commission", df.format(tem * SPUtils.getIntData(CollectionActivity.this, "rate", 0) / 100));
                        pddList.add(gson.fromJson(tempObj.toString(), PDDBean.class));
                    }
                    pddAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            jdAdapterList.notifyDataSetChanged();
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
            super.handleMessage(msg);
        }
    };

    /**
     * @属性:获取京东商品详情
     * @开发者:wmm
     * @时间:2018/12/11 2018/12/11 15:30
     */
    private void getJdGoodsMsgRequest(String ids) {

        String SERVER_URL = "http://api-gw.haojingke.com/index.php/v1/api/jd/goodslist";
        RequestParams params = new RequestParams();
        params.put("apikey",Constants.jd_hjk_appikey);
        params.put("goods_ids",ids);

        HttpUtils.post(SERVER_URL, params, new TextHttpResponseHandler() {

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject data = new JSONObject(responseString);
                    if(data.optInt("status_code") == 200){
                        final JSONArray list = data.getJSONObject("data").getJSONArray("data");
                        handler.sendEmptyMessage(0);
                        int length = list.length();
                        if(page2==1)
                            jdList.clear();
                        for (int i = 0; i < length; i++) {
                            jdList.add(gson.fromJson(list.get(i).toString(), JdGoodsResp.class));
                        }
//                        handler.sendEmptyMessage(0);
                    }else{
                        handler.sendEmptyMessage(0);
                        return;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * @属性:获取淘宝客商品列表
     * @开发者:田哥哥
     * @时间:2018/7/26 17:05
     */
    private void getTbkListRequst() {
        RequestParams requestParams = new RequestParams();
        requestParams.put("p",page1);
        requestParams.put("per",6);
        HttpUtils.post(Constants.MESSAGE_GOODSCOLLECT_GETCOLLECTLIST_URL, requestParams, new onOKJsonHttpResponseHandler<TaobaoGuestBean>(new TypeToken<Response<TaobaoGuestBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onSuccess(int statusCode, Response<TaobaoGuestBean> datas) {
                if (datas.isSuccess()) {
                    List<TaobaoGuestBean.TaobaoGuesChildtBean> list = datas.getData().getList();
                    if(page1==1){
                        taobaoGuesChildtBeans.clear();
                    }
                    taobaoGuesChildtBeans.addAll(list);
                } else {
                    showToast(datas.getMsg());
                }
                shopRecyclerAdapter.notifyDataSetChanged();
                if (refreshLayout != null) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                }
            }
        });
    }
    /**
     * 取消收藏
     * @param position
     */
    private void cancelCollectRequest(final int type,final int position) {
        RequestParams requestParams = new RequestParams();
        requestParams.put("goods_id", type==1?taobaoGuesChildtBeans.get(position).getNum_iid():type==2?pddList.get(position).getGoods_id():jdList.get(position).getGoods_id());
        HttpUtils.post(type==1?Constants.MESSAGE_GOODSCOLLECT_CANCELCOLLECT_URL:type==2?Constants.DE_COLLECT_PDD_GOOD:Constants.DE_COLLECT_JD_GOOD, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onStart() {
                super.onStart();
                showLoadingDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    int code = jsonObject.optInt("code");
                    String msg = jsonObject.optString("msg");
                    if (code == 0) {
                        if(type==1){
                            taobaoGuesChildtBeans.remove(position);
                            shopRecyclerAdapter.notifyDataSetChanged();
                            recyclerView.closeMenu();
                        }else if(type==2){
                            pddList.remove(position);
                            pddAdapter.notifyDataSetChanged();
                            recyclerViewPdd.closeMenu();
                        }else{
                            jdList.remove(position);
                            jdAdapterList.notifyDataSetChanged();
                            recyclerViewJd.closeMenu();
                        }
                    } else {
                        showToast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}